import { XObjectName } from '../../object';
import { Statement, STATEMENT_KIND, STATEMENT_TAG } from '../statement';
import { DropBehavior } from './types';

export class DropFunction<N extends string = string> extends Statement {
  static isDropFunction(object: any): object is DropFunction {
    return (
      Statement.isStatement(object) &&
      object.$kind === STATEMENT_KIND.DROP_FUNCTION
    );
  }
  $kind: STATEMENT_KIND.DROP_FUNCTION = STATEMENT_KIND.DROP_FUNCTION;
  $name: XObjectName<N>;
  $behavior: DropBehavior = 'DROP';
  $tag: STATEMENT_TAG.EXECUTABLE = STATEMENT_TAG.EXECUTABLE;

  constructor(name: XObjectName<N>) {
    super();
    this.$name = name;
  }
}

export function DropFunctionBuilder(name: XObjectName): DropFunction {
  return new DropFunction(name);
}

DropFunctionBuilder.ifExists = function (name: XObjectName): DropFunction {
  const statement = DropFunctionBuilder(name);
  statement.$behavior = 'DROP_IF_EXISTS';
  return statement;
};

export type DropFunctionBuilder = typeof DropFunctionBuilder;
